

package com.excilys.grails
import org.codehaus.groovy.grails.plugins.springsecurity.Secured;
class AlbumController {
    
    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def list = {
        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
        [ albumInstanceList: Album.list( params ), albumInstanceTotal: Album.count() ]
    }

    def show = {
        def albumInstance = Album.get( params.id )

        if(!albumInstance) {
            flash.message = "Album not found with id ${params.id}"
            redirect(action:list)
        }
        else { return [ albumInstance : albumInstance ] }
    }

    @Secured(['ROLE_ADMIN'])
    def delete = {
        def albumInstance = Album.get( params.id )
        if(albumInstance) {
            try {
                albumInstance.delete(flush:true)
                flash.message = "Album ${params.id} deleted"
                redirect(action:list)
            }
            catch(org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "Album ${params.id} could not be deleted"
                redirect(action:show,id:params.id)
            }
        }
        else {
            flash.message = "Album not found with id ${params.id}"
            redirect(action:list)
        }
    }

    @Secured(['ROLE_ADMIN'])
    def edit = {
        def albumInstance = Album.get( params.id )

        if(!albumInstance) {
            flash.message = "Album not found with id ${params.id}"
            redirect(action:list)
        }
        else {
            return [ albumInstance : albumInstance ]
        }
    }

    @Secured(['ROLE_ADMIN'])
    def update = {
        def albumInstance = Album.get( params.id )
        if(albumInstance) {
            if(params.version) {
                def version = params.version.toLong()
                if(albumInstance.version > version) {
                    
                    albumInstance.errors.rejectValue("version", "album.optimistic.locking.failure", "Another user has updated this Album while you were editing.")
                    render(view:'edit',model:[albumInstance:albumInstance])
                    return
                }
            }
            albumInstance.properties = params
            if(!albumInstance.hasErrors() && albumInstance.save()) {
                flash.message = "Album ${params.id} updated"
                redirect(action:show,id:albumInstance.id)
            }
            else {
                render(view:'edit',model:[albumInstance:albumInstance])
            }
        }
        else {
            flash.message = "Album not found with id ${params.id}"
            redirect(action:list)
        }
    }

    @Secured(['ROLE_ADMIN'])
    def create = {
        def albumInstance = new Album()
        albumInstance.properties = params
        return ['albumInstance':albumInstance]
    }

    @Secured(['ROLE_ADMIN'])
    def save = {
        def albumInstance = new Album(params)
        if(!albumInstance.hasErrors() && albumInstance.save()) {
            flash.message = "Album ${albumInstance.id} created"
            redirect(action:show,id:albumInstance.id)
        }
        else {
            render(view:'create',model:[albumInstance:albumInstance])
        }
    }

    def rss = {
        // Chargement des albums
        def albums = Album.list(max:10, sort:'dateCreated', order:'desc')

        // la méthode render est disponible sur tous les contrôleurs
        render(feedType:"rss", feedVersion:"2.0") {
            title = "Flux RSS sur les albums" // le titre du flux
            link = createLink (controller:'album', action:'list', absolute:true) // le lien pour aller sur la liste des albums
            description = "Flux RSS sur les albums" // la description globale du flux

            albums.each() { album -> // pour chaque album dans la liste
                entry(album.name) { // créer une entrée
                    link = createLink(controller:'album', action:'show', id:album.id, absolute:true)
                    // le lien de l'entrée (vers le site en absolu)
                    album.description // et le corps de l'entrée
                }
            }
        }
    }
}
